﻿using LitJson;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using UnityEngine;

public class Item2
{
    public int id;
    public string name;
    public int count;


    public Item2(){}

    public Item2(int id, string name, int count)
    {
        this.id = id;
        this.name = name;
        this.count = count;
    }
}

public class UserInfo2
{
    public string name;
    public int age;
    public bool sex;
    public float valf;
    public double vald;

    public int[] ids;
    public List<int> ids2;
    public Dictionary<string, string> dic;
    public Dictionary<string, string> dic2;

    public Item2 item;
    public Item2 item2;
    public List<Item2> items;

    [SerializeField]
    private int priNumVal;
    [SerializeField]
    protected int proNumVal;

    public void SetVal(int priNumVal, int ProNumVal)
    {
        this.priNumVal = priNumVal;
        this.proNumVal = ProNumVal;
    }
}

public class Role2
{
    public int id;
    public string name;
    public int age;
}

public class Lesson2 : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        #region 序列化
        UserInfo2 user = new UserInfo2();
        user.name = "zabry";
        user.age = 18;
        user.sex = false;
        user.valf = 12.12f;
        user.vald = 2.31215125;
        user.ids = new int[] { 1, 2, 3, 4, 5, 6, 7 };
        user.ids2 = new List<int>() { 10, 20, 30, 40 };
        user.dic = new Dictionary<string, string>() { { "100", "long time no see" }, { "200", "what you name" }, { "300", "goto bed" } };
        user.dic2 = new Dictionary<string, string> { { "a", "aaa" }, { "b", "bbb" }, { "c", "ccc" } };
        user.item = new Item2(10000, "正阳鞋", 1);
        user.item2 = null;
        user.items = new List<Item2>() { new Item2(100, "十字镐", 1), new Item2(101, "木头", 100), new Item2(102, "石头", 130) };
        user.SetVal(1111, 2222);
        string Path1 = Application.persistentDataPath + "/User2.json";
        string userDecode = JsonMapper.ToJson(user);
        File.WriteAllText(Path1, userDecode);
        Debug.LogFormat("Path1:{0}", Path1);
        #endregion

        #region 反序列化
        string jsonStr = File.ReadAllText(Path1);
        //JsonData是LitJson提供的类对象,可以用键值的形式去访问其中的数据
        //JsonData data = JsonMapper.ToObject(jsonStr);
        //方式一(键值访问)
        //Debug.LogFormat("data name:{0} age:{1}", data["name"], data["age"]);
        //方式二(泛型转换)
        UserInfo2 user2 = JsonMapper.ToObject<UserInfo2>(jsonStr);
        Debug.LogFormat("user2 name:{0} age:{1}", user2.name, user2.age);
        #endregion

        #region 读取数据集
        //List
        string Path2 = Application.persistentDataPath + "/Role.json";
        string roleStr = File.ReadAllText(Path2);
        List<Role2> roles = JsonMapper.ToObject<List<Role2>>(roleStr);
        Debug.LogFormat("roles count:{0}", roles.Count);

        //Dictionary
        string Path3 = Application.persistentDataPath + "/Dic.json";
        string DicStr = File.ReadAllText(Path3);
        Dictionary<string,int> dics = JsonMapper.ToObject<Dictionary<string, int>>(DicStr);
        Debug.LogFormat("dics count:{0}", dics.Count);
        #endregion
    }
}
